package com.bootcamp.servlet.listener;

import com.bootcamp.servlet.mvc.model.Product;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebListener
public class AuthLoginListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    public AuthLoginListener() {

    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        // 创建商品
        List<Product> products = new ArrayList<>();
        products.add(new Product(1,"1+", "green", "phone", new BigDecimal("4499.9"), "I am a green phone from 1 plus"));
        products.add(new Product(2,"xiaomi", "black", "tablet", new BigDecimal("1999.9"), "I am a black tablet from xiaomi"));
        products.add(new Product(3,"huawei", "white", "tablet", new BigDecimal("6459.4"), "I am a white tablet from huawei"));
        products.add(new Product(4,"oppo", "orange", "phone", new BigDecimal("499.9"), "I am a orange phone from oppo"));
        products.add(new Product(5,"vivo", "green", "phone", new BigDecimal("999.5"), "I am a green phone from vivo"));
        products.add(new Product(6,"apple", "blue", "laptop", new BigDecimal("14999.5"), "I am a blue mac from apple"));
        products.add(new Product(7,"honor", "purple", "phone", new BigDecimal("4699.8"), "I am a purple phone from honor"));
        products.add(new Product(8,"redmi", "red", "phone", new BigDecimal("1798.5"), "I am a red phone from redmi"));

        // 商品保存在 context域 中
        servletContext.setAttribute("products", products);
        // 读取需要登陆页面的配置文件
        String realPath =servletContext .getRealPath("WEB-INF/config/auth_login_servlet.txt");
        try {
            BufferedReader fileReader = new BufferedReader(new FileReader(realPath));
            List<String> loginServlets =  new ArrayList<String>();
            String line = null;
            while ((line = fileReader.readLine()) != null){
                loginServlets.add(line);
            }
            servletContext.setAttribute("loginservlets",loginServlets);
            fileReader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        /* This method is called when the servlet Context is undeployed or Application Server shuts down. */
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is added to a session. */
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is removed from a session. */
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is replaced in a session. */
    }
}
